import argparse
import json
import os
from scanerUtils import ScanerUtils

# 解析参数
parser = argparse.ArgumentParser(description='帮助文档')
parser.add_argument('-p', '--project', type=str, help='指定项目文件夹')
parser.add_argument('-s', '--skip', nargs='+', default=[], help='指定要跳过的文件夹数组')
parser.add_argument('-w', '--whitelist_file', type=str, help='指定白名单文件')
args = parser.parse_args()

# 检查必要参数
if not args.project:
    print("请指定项目文件夹")
    exit(1)

if not args.whitelist_file:
    print("whitelist不能为空")
    exit(1)

print(args.project)
default_ignore_folder = [".git", "Carthage"]

# 读取白名单json文件内容
with open(args.whitelist_file, "r") as file:
    whitelist_json = file.read()

# 解析JSON为数组
whitelist = json.loads(whitelist_json)["frameworks"]

print(whitelist)

source_files = ScanerUtils().find_swift_folders(args.project, default_ignore_folder)
print(source_files)


unvalidFileDictionary = dict()
for file in source_files:
    frameworksSet = ScanerUtils.scanSwiftFile(file)
    unvalidFrameworks = []
    for framework in frameworksSet:
        if framework not in whitelist:
            unvalidFrameworks.append(framework)
    
    if len(unvalidFrameworks)  > 0:
        unvalidFileDictionary[file] = unvalidFrameworks


    
if len(unvalidFileDictionary) > 0:
    # 使用json.dumps()进行格式化打印
    pretty_dict = json.dumps(unvalidFileDictionary, indent=4, separators=(', ', ': '))
    print(pretty_dict)
    exit(1)
